﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for PageBase
/// </summary>
public class PageBase : System.Web.UI.Page
{
    protected FormMode formMode = FormMode.Default;
    protected Guid sessionGuid = Guid.Empty;

    public PageBase()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    protected string buildForwardUrl(string url)
    {
        string _url = url;
        string _formParameters = string.Empty;

        if (_url.EndsWith("?") ||
            _url.EndsWith("&"))
        {
        }
        else
        {
            if (_url.Contains("?"))
            {
                _url += "&";
            }
            else
            {
                _url += "?";
            }
        }

        string _queryStringParameterName = "SessionGuid";

        if (Request.QueryString[_queryStringParameterName] != null)
        {
            _formParameters += string.Format("{0}={1}&",
                _queryStringParameterName,
                System.Web.HttpUtility.UrlEncode(Request.QueryString[_queryStringParameterName]));
        }

        return string.Format("{0}{1}",
            _url,
            _formParameters);
    }

    protected void initializeWebForm()
    {
        if (Request.QueryString["FormMode"] != null)
        {
            string _formModeString = Request.QueryString["FormMode"];

            if (_formModeString.ToLower() == "create")
            {
                formMode = FormMode.Create;
            }
            else if (_formModeString.ToLower() == "edit")
            {
                formMode = FormMode.Edit;
            }
            else if (_formModeString.ToLower() == "delete")
            {
                formMode = FormMode.Delete;
            }
            else
            {
                formMode = FormMode.Default;
            }
        }
        else
        {
            formMode = FormMode.Default;
        }

        if (Request.QueryString["SessionGuid"] != null)
        {
            sessionGuid = new Guid(Request.QueryString["SessionGuid"]);
        }
        else
        {
            sessionGuid = Guid.Empty;
        }
    }
}
